package dao;

import java.util.ArrayList;
import java.util.List;

import entity.StudentInfo;

public class StudentInfoDao extends BaseDao {

	/**
	 * 查询所有学生
	 * 
	 * @return
	 */
	public List<StudentInfo> selectAll() {
		try {
			rs = executeQuery("select * from studentinfo order by sid desc");
			List<StudentInfo> stuList = new ArrayList<StudentInfo>();
			while (rs.next()) {
				StudentInfo stu = new StudentInfo();
				stu.setSid(rs.getInt("sid"));
				stu.setSname(rs.getString("sname"));
				stu.setSgender(rs.getString("sgender"));
				stu.setSage(rs.getInt("sage"));
				stu.setSaddress(rs.getString("saddress"));
				stu.setSemail(rs.getString("semail"));
				stuList.add(stu);
			}
			return stuList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据学生id查询学生
	 * 
	 * @param sid
	 * @return
	 */
	public StudentInfo getStudentInfoBySid(int sid) {
		try {
			rs = executeQuery("select * from studentinfo where sid = ?", sid);
			if (rs.next()) {
				StudentInfo stu = new StudentInfo();
				stu.setSid(rs.getInt("sid"));
				stu.setSname(rs.getString("sname"));
				stu.setSgender(rs.getString("sgender"));
				stu.setSage(rs.getInt("sage"));
				stu.setSaddress(rs.getString("saddress"));
				stu.setSemail(rs.getString("semail"));
				return stu;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 更新学生
	 * @param stu
	 * @return
	 */
	public int updateStu(StudentInfo stu){
		return executeUpdate("update studentinfo set sname=?,sgender=?,sage=?,saddress=?,semail=? where sid = ?",
				stu.getSname(),stu.getSgender(),stu.getSage(),
				stu.getSaddress(),stu.getSemail(),stu.getSid()
				);
	}
	
	/**
	 * 根据id删除学生 
	 * @param sid
	 * @return
	 */
	public int delStuBySid(int sid){
		return executeUpdate("delete from studentinfo where sid = ?", sid);
	}
}
